package main

import (
	"fmt"
	"sort"
)

func main() {
	//切片定义
	//var name []T
	var a []string
	var b = []int{}
	var c = []bool{false, true}
	var d = [5]int{1, 2, 3, 4, 5}
	e := d[1:3]
	fmt.Println(b)
	fmt.Println(c == nil)
	fmt.Println(a == nil)
	fmt.Printf("len = %d", len(c))
	fmt.Printf("e: %v len:%v cap:%v\n", e, len(e), cap(e))
	fmt.Printf("d: %v len:%v cap:%v\n", d, len(d), cap(d))
	s := d[1:3:4]
	fmt.Printf("s: %v len:%v cap:%v", s, len(s), cap(s))
	//使用len() == 0 来判断切片是否为空
	//使用make函数构造切片
	newS := make([]int, 3, 5)
	fmt.Printf("newS: %v len:%v cap:%v", newS, len(newS), cap(newS))
	fmt.Println("------")
	//切片不能直接比较，可以通过对nil比较
	if newS == nil {
		fmt.Println("newS is nil")
	}
	fmt.Println()
	//切片的拷贝
	newS2 := newS
	newS2[0] = 1
	fmt.Println(newS)
	fmt.Println(newS2)
	fmt.Println()
	//切片的遍历
	for i, v := range newS2 {
		fmt.Println(i, v)
	}
	return
	//append为切片添加元素
	newS2 = append(newS2, 4, 30)
	fmt.Println(newS2)
	//切片的扩容是之前的2倍
	var numSlice []int
	for i := 0; i < 10; i++ {
		numSlice = append(numSlice, i)
		fmt.Printf("%v, len:%d, cap:%d, ptr:%p\n", numSlice, len(numSlice), cap(numSlice), numSlice)
	}
	//数组copy
	srcSlice := []int{1, 2, 3, 4, 5}
	destSlice := make([]int, 5, 5)
	copy(destSlice, srcSlice)
	println(destSlice)

	//从切片删除元素
	aaa := []int{30, 31, 45, 60, 32, 33, 34}
	aaa = append(aaa[:2], aaa[3:]...) //取出来重新赋值
	sort.Ints(aaa)
	fmt.Println(aaa)

}
